package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.ConsensusGuideMapper;
import com.zhijian.medical.entity.po.ConsensusGuide;
import com.zhijian.medical.entity.vo.request.ConsensusGuideSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.ConsensusGuideSearchVo;
import com.zhijian.medical.entity.vo.response.ConsensusGuideVo;
import com.zhijian.medical.service.ConsensusGuideService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 共识指南业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-05-09 17:51:56
 */
@Slf4j
@Service
public class ConsensusGuideServiceImpl implements ConsensusGuideService {

    @Autowired
    private ConsensusGuideMapper mapper;

    @Override
    public ConsensusGuide selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ConsensusGuideVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(ConsensusGuide consensusGuide) {
        Date now = new Date();
        consensusGuide.setCreateTime(now);
        consensusGuide.setUpdateTime(now);
        return mapper.insert(consensusGuide) > 0 ? consensusGuide.getId() : 0L;
    }

    @Override
    public boolean update(ConsensusGuide consensusGuide) {
        Date now = new Date();
        consensusGuide.setUpdateTime(now);
        return mapper.updateByPrimaryKey(consensusGuide) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(ConsensusGuide record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(ConsensusGuideSaveOrUpdateVo saveOrUpdateVo) {
        ConsensusGuide consensusGuide = new ConsensusGuide();
        BeanUtils.copyProperties(saveOrUpdateVo, consensusGuide);
        return this.insert(consensusGuide) != 0;
    }

    @Override
    public boolean modify(ConsensusGuideSaveOrUpdateVo saveOrUpdateVo) {
        ConsensusGuide old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        ConsensusGuide consensusGuide = new ConsensusGuide();
        BeanUtils.copyProperties(saveOrUpdateVo, consensusGuide);
        return this.update(consensusGuide);
    }

    @Override
    public ConsensusGuideVo selectOne(ConsensusGuideSearchVo consensusGuideSearchVo) {
        return mapper.selectOne(consensusGuideSearchVo);
    }

    @Override
    public Integer count(ConsensusGuideSearchVo consensusGuideSearchVo) {
        return mapper.count(consensusGuideSearchVo);
    }

    @Override
    public List<ConsensusGuideVo> selectList(ConsensusGuideSearchVo consensusGuideSearchVo) {
        return mapper.selectList(consensusGuideSearchVo);
    }

    @Override
    public Page<ConsensusGuideVo> selectPage(ConsensusGuideSearchVo consensusGuideSearchVo) {
        AssertUtil.check(Objects.isNull(consensusGuideSearchVo.getPageNum()) || Objects.isNull(consensusGuideSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(consensusGuideSearchVo);
        //分页
        Page<ConsensusGuideVo> page = new Page<>(true, count, consensusGuideSearchVo.getPageNum(), consensusGuideSearchVo.getPageSize());
        consensusGuideSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ConsensusGuideVo> responseVos = this.selectList(consensusGuideSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<ConsensusGuide> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(ConsensusGuideSearchVo consensusGuideSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(consensusGuideSearchVo);
    }

}